package com.learn.activiti.service;

import com.learn.activiti.condition.IdeaCompleteCondition;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <一句话描述>
 *
 * @author xianglujun
 * @date 2019/7/10 15:57
 */
@Service
@Slf4j
public class DeployService {

    private RepositoryService repositoryService;
    private RuntimeService runtimeService;
    private TaskService taskService;
    private IdeaCompleteCondition completeCondition;

    public DeployService(RepositoryService repositoryService,
                         RuntimeService runtimeService,
                         TaskService taskService,
                         IdeaCompleteCondition completeCondition) {
        this.repositoryService = repositoryService;
        this.runtimeService = runtimeService;
        this.taskService = taskService;
        this.completeCondition = completeCondition;
    }

    @PostConstruct
    public void init() {
//        deployXmlFile();
//        startProcess();

//        deployOutOwnXmlFile();
    }

    public void deployXmlFile() {
        log.info("deploy bpmn20 xml file for activiti..");
        repositoryService.createDeployment()
                .name("vacationRequest")
                .addClasspathResource("processes/VacationRequest.bpmn20.xml")
//                .addClasspathResource("processes/VacationRequest.png")
                .deploy();

        log.info("deploy done");
    }

    public void startProcess() {
        log.info("启动一个流程...");
        // 向流程中传入参数
        Map<String, Object> variableMap = new HashMap<>();
        variableMap.put("employeeName", "Kermit");
        variableMap.put("numberOfDays", new Integer(4));
        variableMap.put("vacationMotivation", "I'm really tired!");

        String deploymentId =
                runtimeService.startProcessInstanceByKey("vacationRequest", variableMap).getProcessDefinitionId();
        log.info("deployment id : {}", deploymentId);

        // 验证启动实例是否成功
        log.info("Number of process instances count: {}", runtimeService.createExecutionQuery().count());
    }

}